﻿using System;
using System.Text;

namespace SmartFramework.Test {
	public class IpConverter {
		public static String Long2Ip(long ipLong) {
			var ipInfo = new StringBuilder();
			var mask = new long[] { 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 };
			for (int i = 0; i < 4; i++) {
				long num = (ipLong & mask[i]) >> (i * 8);
				if (i > 0) ipInfo.Insert(0, ".");
				ipInfo.Insert(0, num);
			}

			return ipInfo.ToString();
		}

		public static long Ip2Long(String ip)
		{
			String[] ips = ip.Split('.');
			foreach (var s in ips)
			{
				Console.WriteLine(s);
			}
			long num = 0;
			int maxIndex = ips.Length - 1;

			for (int i = 0; i <= maxIndex; i++) {
				num += (int.Parse(ips[i]) << ((maxIndex - i) * 8));
			}

			return num;
		}
	}
}
